﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using FinalTestMachine.Motion.Aixs.Light;
using FinalTestMachine.Motion.IO;
using FinalTestMachine.View.Dispenser;
using LanCustomControldllExtend.Unit_Aix;
using static LanCustomControldll.Unit_Aix.UC_SimpleCalcMode;

namespace FinalTestMachine.View
{
    /// <summary>
    /// Motion 窗口，界面管理
    /// </summary>
    class MotionWndViewModelHelper : LanguageControlBase.Wpf.BindableObject
    {
        static MotionWndViewModelHelper Cur = new MotionWndViewModelHelper();

        //这里添加用户界面
        private MotionWndViewModelHelper()
        {
            //注册按钮按下发生时，获取对应按钮的界面
            Unit_UserViewModel.actionButtonDownEventHandler += () => { UserControlCurShow = Unit_UserViewModel.userControl_CurShow.userControl; };

            void AddPage(AbstractSingleAixdll.AbstractSingleAix mSingleAix, string buttonCaption)
            {
                LanCustomControldll.Unit_Aix.UC_DemoAix demoAixViewModel = new LanCustomControldll.Unit_Aix.UC_DemoAix();
                demoAixViewModel.SetAixInfo(mSingleAix);

                Unit_UserViewModel unit_UserViewModel_DemoAix = new Unit_UserViewModel(buttonCaption, demoAixViewModel, demoAixViewModel);
            }

            //添加轴
            AddPage(Motion.Aixs.Aix_Turn.Cur, "旋转主轴");

            AddPage(Motion.Aixs.Site2.Aix_Site2_PlaneSourceLift.Cur, "Site2平面光源");
            AddPage(Motion.Aixs.Site2.Aix_Site2_RelayMirror.Cur, "Site2中继镜");

            AddPage(Motion.Aixs.Site4.Aix_Site4_PlaneSourceLift.Cur, "Site4平面光源");
            AddPage(Motion.Aixs.Site4.Aix_Site4_RelayMirror.Cur, "Site4中继镜");

            AddPage(Motion.Aixs.Site6.Aix_Site6_PipeUpDown.Cur, "Site6光桶");

            /*#region 光管
            LanCustomControldll.UC_TableList uC_TableList_Light = new LanCustomControldll.UC_TableList();
            void TableLightAixsAdd(Aix_LightBase1 mSingleAix, string headerName)
            {
                LanCustomControldll.Unit_Aix.UC_DemoAix demoAixViewModel = new LanCustomControldll.Unit_Aix.UC_DemoAix();
                demoAixViewModel.SetAixInfo(mSingleAix);
                LanCustomControldll.Unit_Aix.UC_SimpleCalcMode uC_SimpleCalc = new LanCustomControldll.Unit_Aix.UC_SimpleCalcMode();
                uC_SimpleCalc.SetBinding(mSingleAix.CalculaorParam);
                demoAixViewModel.SetPosExtral(uC_SimpleCalc);
                uC_TableList_Light.Add(headerName, demoAixViewModel);
                ExtraShowUIEventHandler += demoAixViewModel.ShowUI;

            }

            void TableLightAixsAdd1(Aix_LightBase2 mSingleAix, string headerName)
            {
                LanCustomControldll.Unit_Aix.UC_DemoAix demoAixViewModel = new LanCustomControldll.Unit_Aix.UC_DemoAix();
                demoAixViewModel.SetAixInfo(mSingleAix);
                View.AixCalcView.UC_SimpleCalcMode uC_SimpleCalc = new View.AixCalcView.UC_SimpleCalcMode();
               
                uC_SimpleCalc.SetBinding(mSingleAix.CalculaorParam);
                demoAixViewModel.SetPosExtral(uC_SimpleCalc);
                uC_TableList_Light.Add(headerName, demoAixViewModel);
                ExtraShowUIEventHandler += demoAixViewModel.ShowUI;

            }

            int index_light = 1;
            foreach (var item in Aix_LightBaseBase.LightList)
            {
                if(item is Aix_LightBase1)
                {
                    TableLightAixsAdd(item as Aix_LightBase1, (index_light++).ToString());

                }
                else
                {
                    TableLightAixsAdd1(item as Aix_LightBase2, (index_light++).ToString());

                }
            }
            Unit_UserViewModel unit_UserView_lightsList = new Unit_UserViewModel("光管轴", uC_TableList_Light);
            #endregion*/



            //IO 界面
            LanCustomControldllExtend.Unit_Io.UC_DemoIOList_Format5_Extern uC_DemoIOList_Format1 = new LanCustomControldllExtend.Unit_Io.UC_DemoIOList_Format5_Extern(new StaticIOHelper());

            //更新为从DLL中读取IO卡信息
            uC_DemoIOList_Format1.SetIoPages(AM.Core.IO.IOBuilder.IoMsg);

            Unit_UserViewModel unit_UserViewModel_io = new Unit_UserViewModel("IO", uC_DemoIOList_Format1, uC_DemoIOList_Format1);



            //添加其他参数管理界面
            Unit_OtherParamSetting unit_OtherParamSettingViewModel = new Unit_OtherParamSetting();
            unit_OtherParamSettingViewModel.SetParam(Motion.OtherParamHelper.Cur);
            unit_OtherParamSettingViewModel.btn_SaveParam.SetSaveClick(delegate
            {
                try
                {
                    Motion.OtherParamHelper.Cur.Save();

                }
                catch (Exception ex)
                {

                    MessageBox.Show(ex.Message);
                }

            });
            Unit_UserViewModel unit_UserViewModel_otherParam = new Unit_UserViewModel("OtherParameters", unit_OtherParamSettingViewModel, unit_OtherParamSettingViewModel);

            //View.MES.UserControlMESConfig userControlMESConfig = new MES.UserControlMESConfig();
            //Unit_UserViewModel unit_UserViewModel_MES = new Unit_UserViewModel("MES", userControlMESConfig);

            //设置默认显示
            unit_UserViewModelsList[0].SetShow();
            UserControlCurShow = unit_UserViewModelsList[0].userControl;
        }


        public static MotionWndViewModelHelper CreateInstrance()
        {
            return Cur;
        }

        /// <summary>
        /// 已经创建的界面集合
        /// </summary>
        public ObservableCollection<Unit_UserViewModel> unit_UserViewModelsList => Unit_UserViewModel.userControlsListHaved;
        bool IsExit = false;
        Thread task_Curr;
        /// <summary>
        /// 额外的界面刷新事件
        /// </summary>
        public event Action ExtraShowUIEventHandler;


        UserControl userControlCurShow;
        public UserControl UserControlCurShow
        {
            get { return userControlCurShow; }
            set
            {
                SetProperty(ref userControlCurShow, value);
            }
        }

        public void Initial()
        {
            if (task_Curr != null) return;
            task_Curr = new Thread(ShowUI);
            task_Curr.IsBackground = true;
            task_Curr.Start();

        }

        public void Close()
        {
            IsExit = true;
            if (task_Curr != null)
            {
                int it = 0;
                while (IsExit && task_Curr.IsAlive)
                {
                    it++;
                    Thread.Sleep(10);
                    if (it > 100)
                    {
                        task_Curr.Abort();
                        break;
                    }
                }

            }


        }

        public void ShowUI()
        {
            try
            {
                bool IsScan = false;
                while (true)
                {
                    if (IsExit)
                    {
                        IsExit = false;
                        return;
                    }
                    IsScan = false;
                    App.Current.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        try
                        {
                            Unit_UserViewModel.Fun_AllShow();
                            ExtraShowUIEventHandler?.Invoke();
                        }
                        catch (Exception ex)
                        {
                            Motion.StaticInitial.Motion.WriteErrToUser("UI Scan Err:" + ex.Message);
                        }

                        IsScan = true;
                    }));
                    Thread.Sleep(100);
                    while (!IsScan && !IsExit)
                    {
                        Thread.Sleep(100);
                    }



                }
            }
            finally
            {
                task_Curr = null;
            }

        }



        ~MotionWndViewModelHelper()
        {
            Close();

            task_Curr?.Abort();
        }

    }
}
